<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace Utilities\Geolocation;

class GeolocationService
{

    /**
     * 
     * @var string
     */
    private $databasePath;
    
    /**
     * 
     * @param string $databasePath
     */
    public function __construct($databasePath) 
    {
        $this->databasePath = $databasePath;
    }

    /**
     * 
     * @param string $ip
     * @return string
     */
    public function getCountryCode($ip) 
    {
        $geoIp = $this->getGeoIp();
        
    	$countryCode = geoip_country_code_by_addr($geoIp, $ip);
    	$this->closeGeoIp($geoIp);
    	
    	return $countryCode;
    }

    /**
     * 
     */
    private function getGeoIp() 
    {
        return geoip_open($this->databasePath, GEOIP_MEMORY_CACHE);
    }
    
    /**
     * 
     */
    private function closeGeoIp($geoIp) 
    {
    	geoip_close($geoIp);
    }
}

?>